﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZetaHtmlTidy;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Drawing;
using System.Xml.Linq;

namespace CarInfoBusiness
{
    public class Common
    {
        private static Common com;

        public static  Common Instance
        {
            get {
                if (com == null)
                    com = new Common();
                return com; 
            }
        }

    
        private Common()
        {
 
        }

        public string GetHtml(string url)
        {
            WebRequest request = WebRequest.Create(url);
            request.UseDefaultCredentials = false;
            WebResponse response = request.GetResponse();
            Stream resStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, System.Text.Encoding.GetEncoding("gbk"));
            string str = sr.ReadToEnd();
            resStream.Close();
            sr.Close();
            return str;
        }

        public System.Drawing.Image GetPic(string url)
        {
            WebRequest myWebRequest = WebRequest.Create(url);

            myWebRequest.Headers.Add("Cookie", "ASP.NET_SessionId=tr0p4g45xa0qzg45bmlqprnn");
            WebResponse myWebResponse = myWebRequest.GetResponse();
            Stream ReceiveStream = myWebResponse.GetResponseStream();
            Bitmap map = new Bitmap(ReceiveStream, false);
            
            return (System.Drawing.Image)map;
        }

        //public static System.Data.Linq.Binary GetPic(string url)
        //{
        //    WebRequest myWebRequest = WebRequest.Create(url);

        //    myWebRequest.Headers.Add("Cookie", "ASP.NET_SessionId=tr0p4g45xa0qzg45bmlqprnn");
        //    WebResponse myWebResponse = myWebRequest.GetResponse();
        //    Stream ReceiveStream = myWebResponse.GetResponseStream();


        //}
        public byte[] ImageToByteArray(System.Drawing.Image image)
        {
            //实例化流
            MemoryStream imageStream = new MemoryStream();
            //将图片的实例保存到流中   
            image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Bmp);
            //保存流的二进制数组
            byte[] imageContent = new Byte[imageStream.Length];

            imageStream.Position = 0;
            //将流泻如数组中
            imageStream.Read(imageContent, 0, (int)imageStream.Length);

            return imageStream.ToArray();

        }

        public XElement GetXml(string Url)
        {
            string StrXml = GetHtml(Url);

            using (HtmlTidy tidy = new HtmlTidy())
            {
                StrXml = tidy.CleanHtml(StrXml, HtmlTidyOptions.ConvertToXhtml);
            }
            StrXml = StrXml.Replace("&nbsp;", "");
            //StrXml = StrXml.Replace("<br>", "<br/>");
            //StrXml = StrXml.Replace("language=javascript", "language=\"javascript\"");
            //StrXml = StrXml.Replace("&ldquo;", "");
            //StrXml = StrXml.Replace("&rdquo;", "");

            //StrXml = StrXml.Replace("<p>", "");
            //StrXml = StrXml.Replace("</p>", "");
            //StrXml = StrXml.Replace("type=text/javascript", "type=\"text/javascript\"");
            //StrXml = StrXml.Replace("<center>", "");
            //if (Url.StartsWith("http://db.chinacars.com/11/"))
            //    StrXml = StrXml.Replace("</center>", "</div>");
            //else
            //    StrXml = StrXml.Replace("</center>", "");

            //StrXml = StrXml.Replace("&", ";");

            //StrXml = StrXml.Replace("<div class=\"left_13s\">", "<div class=\"left_13s\"><div>");
            //if (Url.StartsWith("http://db.chinacars.com/12/"))
            //    StrXml = StrXml.Replace("<body>", "<body><div><div>");

            //StrXml = StrXml.Replace("<img width=500 height=375", "<img width=\"500\" height=\"375\"");
            //StrXml = StrXml.Replace("target=_blank", "target=\"_blank\"");

            //StrXml = StrXml.Replace("td width=\"85%\"", "td");
            //StrXml = StrXml.Replace("</a>(<sup>0</sup>)", "");



            try
            {
                
                XElement xml = XElement.Parse(StrXml);
                return xml;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                TextOutput("Capture Url: " + Url + " Failed");
                TextOutput(ex.Message);
                return null;
            }
        }

        private Append2Text a2t;

        public void RegTextOutput(Append2Text _a2t)
        {
            a2t = _a2t;
        }

        public void TextOutput(string msg)
        {
            a2t(msg);
        }

        private ChangeProcessBarValue cpbv;

        public void RegProcesssBarValue(ChangeProcessBarValue _cpbv)
        {
            cpbv = _cpbv;
        }

        public void ChangeProcessBarValue(double value)
        {
            cpbv(value);
        }
    }

    public delegate void Append2Text(string msg);

    public delegate void ChangeProcessBarValue(double value);
}
